import { TimerUtils } from "../../api/timer/TimerUtils";
import { E_TimeConfig } from "../defs/com/E_TimeConfig";
import { E_RandomEventStatus } from "../defs/random_event/E_RandomEventStatus";
import { E_RandomEventType } from "../defs/random_event/E_RandomEventType";
import { E_ShopId } from "../defs/shop/E_ShopId";
import { Player } from "../struct/Player";
import { RandomEvent } from "../struct/RandomEvent";
import { RandomEventItem } from "../struct/RandomEventItem";


/**
 * 数据-随机事件
 */
export abstract class SDRandomEvent {



    /**
     * 获取指定类型的事件组
     * @param player 
     * @param eventType 
     * @param outs 
     * @returns 
     */
    public static getEventListByType(
        player: Player,
        eventType: E_RandomEventType,
        outs: RandomEventItem[] = []
    ): RandomEventItem[] {

        const vs = player.randomEvent.getEventValues();

        for (let i = 0, len = vs.length; i < len; i++) {
            const event = vs[i];
            if (event.status === E_RandomEventStatus.Active && event.randomEventType === eventType) {
                outs.push(event);
            }
        }

        return outs;
    }



    /**
     * 随机商城是否开启
     * @param player 
     * @param shopId 
     * @returns 
     */
    public static isRandomEventShopOpen(
        player: Player,
        shopId: E_ShopId
    ): boolean {

        const shopEventType = this.getShopTypeIdTrans(shopId);

        if (shopEventType !== E_RandomEventType.None) {
            const list = player.randomEvent.getEventValues();
            for (let i = list.length - 1; i >= 0; i--) {
                const event = list[i];
                if (event.status === E_RandomEventStatus.Active && event.randomEventType === shopEventType) {
                    return true;
                }
            }
            return false;
        }

        return true;
    }


    /**
     * 获取商城类型转换
     * @param id 
     * @returns 
     */
    private static getShopTypeIdTrans(id: E_ShopId): E_RandomEventType {
        switch (id) {
            case E_ShopId.Mystery: return E_RandomEventType.MysticalShop;
        }
        return E_RandomEventType.None;
    }


    /**
     * 强制刷新时间
     * @param time 
     */
    public static getSystemResetTime(time: number): number {
        let t = TimerUtils.getDayZeroTime(time);
        t += E_TimeConfig.Hour * 5;
        return t;
    }


    /**
     * 配置在主页上的位置
     * @param re 
     * @param configId 
     * @returns 位置
     */
    public static getConfigPosInMainPage(
        re: RandomEvent,
        configId: number
    ): number {

        let event = re.getEvent(re.pos1EventId);
        if (event && event.configId === configId) {
            return 1;
        }
        event = re.getEvent(re.pos2EventId);
        if (event && event.configId === configId) {
            return 2;
        }
        event = re.getEvent(re.pos3EventId);
        if (event && event.configId === configId) {
            return 3;
        }
        return -1;
    }


    /**
     * 事件在主页上的位置
     * @param re 
     * @param eventId 
     * @returns 
     */
    public static getEventPosInMainPage(
        re: RandomEvent,
        eventId: number
    ): number {
        if (eventId === re.pos1EventId) {
            return 1;
        }
        if (eventId === re.pos2EventId) {
            return 2;
        }
        if (eventId === re.pos3EventId) {
            return 3;
        }
        return -1;
    }


}
